<?php

/**
 * @author xxxxxx
 * @copyright 2008
 */
 
class eventHooks
{
	public $hooks = array(); //todo:must be protected //DEBUG

	function __construct(){}
	
	function __destruct(){}
	
	function register($hook)
	{
		$this->hooks[$hook]=array();
	}
	
	function unregister($hook)
	{
		unset($this->hooks[$hook]);
	}
	
	function attach($hook, $callback, $parameters=array(), $position='end', $positionRelativeTo=null)
	{
		
		$callbackWithParameters = array('callback'=>$callback, 'parameters'=>$parameters);
		
		switch($position)
		{
			case 'start':
				$this->hooks[$hook] = array_reverse($this->hooks[$hook]);
				$this->hooks[$hook][] = $callbackWithParameters;
				$this->hooks[$hook] = array_reverse($this->hooks[$hook]);
			break;
			case 'before':
				$this->hooks[$hook] = xarray::inject($this->hooks[$hook], xarray::getPosByVal($this->hooks[$hook], $positionRelativeTo)+1, $callbackWithParameters);
			break;
			case 'after':
				$this->hooks[$hook] = xarray::inject($this->hooks[$hook], xarray::getPosByVal($this->hooks[$hook], $positionRelativeTo)+2, $callbackWithParameters);
			break;
			case 'end':
				$this->hooks[$hook][] = $callbackWithParameters;
			break;
		}
	}
	
	function detach($hook, $callback)
	{
		unset($this->hooks[$hook][$callback]);
	}
	
	function reset()
	{
		//unset($this->hooks);
	}

//	function fire($hook)
//	{
//		foreach ($this->hooks[$hook] as $hooks_row)
//		{
//			call_user_func_array($hooks_row['callback'], @array_merge(array($this), $hooks_row['parameters']));
//		}
//	}
}

?>